1
|
|
|
'use strict' |
2
|
|
|
|
3
|
|
|
import DialogComponent from './components/dialog.vue' |
4
|
|
|
import {DIALOG_TYPES, DEFAULT_OPTIONS} from './js/constants' |
5
|
|
|
import Directives from './js/directives' |
6
|
|
|
import {mergeObjs} from './js/utilities' |
7
|
|
|
|
8
|
|
|
let Plugin = function(Vue, globalOptions = {}){ |
9
|
|
|
this.globalOptions = globalOptions |
10
|
|
|
|
11
|
|
|
this.Dialog = (() => { |
12
|
|
|
let DialogConstructor = Vue.extend(DialogComponent) |
13
|
|
|
let node = document.createElement("div") |
14
|
|
|
document.querySelector('body').appendChild(node) |
15
|
|
|
|
16
|
|
|
return (new DialogConstructor()).$mount(node) |
17
|
|
|
})() |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
Plugin.prototype.alert = function(message = null, options = {}){ |
21
|
|
|
message && (options.message = message) |
22
|
|
|
return this.open(DIALOG_TYPES.ALERT, options) |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
Plugin.prototype.confirm = function(message = null, options = {}){ |
26
|
|
|
message && (options.message = message) |
27
|
|
|
return this.open(DIALOG_TYPES.CONFIRM, options) |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
Plugin.prototype.open = function(type, localOptions = {}){ |
31
|
|
|
return new Promise((resolve, reject) => { |
32
|
|
|
|
33
|
|
|
localOptions.id = 'dialog.' + Date.now() |
34
|
|
|
localOptions.window = type |
35
|
|
|
localOptions.promiseResolver = resolve |
36
|
|
|
localOptions.promiseRejecter = reject |
37
|
|
|
|
38
|
|
|
this.Dialog.commit(mergeObjs(DEFAULT_OPTIONS, this.globalOptions, localOptions)) |
39
|
|
|
}) |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
Plugin.install = function (Vue, options) { |
43
|
|
|
|
44
|
|
|
let DirectivesObj = new Directives(Vue) |
45
|
|
|
|
46
|
|
|
Vue.directive('confirm', DirectivesObj.confirmDefinition) |
47
|
|
|
|
48
|
|
|
//Vue.directive('alert', DirectivesObj.alertDefinition) |
49
|
|
|
|
50
|
|
|
Vue.dialog = new Plugin(Vue, options) |
51
|
|
|
|
52
|
|
|
Object.defineProperties(Vue.prototype, { |
53
|
|
|
$dialog: { |
54
|
|
|
get () { |
55
|
|
|
return Vue.dialog |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
}) |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
export default Plugin |